$(function(){
    var key = getCookie('key');
    var store_id = getCookie('store_id');
    if(key==null){
        window.location.href = 'login.html';
    }
    if(!store_id){
        window.location.href = WapSiteUrl + '/tmpl/member/member.html';
    }
    var username = '';

    if(key){
        url = "/mobile/index.php?act=store&op=storeGoodsList";
        $.ajax({
            type:"GET",
            url:url,
            data:{key:key,store_id:store_id},
            dataType:"json",
            success:function(data){
                if (data['code']==200){
                    data = data['datas']['goodslist'];
                    var html = "";
                    for(var i=0;i<data.length;i++){
                        html += '<li><img class="img1" src="'+data[i]['goods_image']+'" alt=""/><div class="center"><p>'+data[i]['goods_name']+'</p><p>价格:<span class="num">￥'+data[i]['goods_price']+'</span></p></div><a href="javascript:;" onclick="delGoodsFromStore(this,'+data[i]['goods_id']+')">删除</a></li>'
                    }
                    document.getElementsByClassName('list')[0].innerHTML = html;
                }
            }
        })

        /**
         *  获取自选商品列表
         */
        var page = 1;
        $(window).scroll(function () {
            var windowheight = $(window).height();//窗口高度
            var scrolltop = $('body').scrollTop();//滚动条高度
            var eh = $(document).height();//文本高度
            console.log(111);
            if (parseInt(windowheight + scrolltop)+2 >= eh) {
                page++;
                url = "/mobile/index.php?act=store&op=storeGoodsList&page="+page;
                $.ajax({
                    type:"GET",
                    url:url,
                    data:{key:key},
                    dataType:"json",
                    success:function(data){
                        if (data['code']==200){
                            data = data['datas']['goodslist'];
                            var html = "";
                            for(var i=0;i<data.length;i++){
                                html += '<li><img class="img1" src="'+data[i]['goods_image']+'" alt=""/><div class="center"><p>'+data[i]['goods_name']+'</p><p>价格:<span class="num">￥'+data[i]['goods_price']+'</span></p></div><a href="javascript:;" onclick="delGoodsFromStore(this,'+data[i]['goods_id']+')">删除</a></li>'
                            }
                            document.getElementsByClassName('list')[0].innerHTML = html;
                        }
                    }
                })

                //alert('123');
            }
        })

    }else{
        // 未登录处理
        window.location.href = "login.html";
    }
})

/**
 * 从我的小店里删除
 */
function delGoodsFromStore(obj,id){
    url = "/mobile/index.php?act=store&op=delgoods";
    var objparent = obj.parentNode;
    $.ajax({
        type:"POST",
        url:url,
        data:{id:id},
        dataType:"text",
        success:function(data){
            if (data==1){
                document.getElementsByClassName('list')[0].removeChild(objparent);
            }else{
                alert('删除失败')
            }
        }
    })
}